package com.aspire.moss.portal.view;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;

import com.aspire.moss.portal.view.Workspace.OnTouthEvent;

/**
 * 整个桌面的业务逻辑
 * @author Administrator
 *
 */
public class DragController implements OnTouthEvent {
	
	private static final String TAG = "DragController";

	Context mContext;
	
	Workspace workspace;
	ClipImageView clipImageView;
	WorkspaceTouchEvent touchEvent ; 
	
	
	public DragController(Context mContext){
		this.mContext = mContext;
		touchEvent = new WorkspaceTouchEvent();
	}
	
	@Override
	public boolean onWorspaceTouchEvent(MotionEvent event,Workspace workspace) {
		return touchEvent.onWorspaceTouchEvent(event, workspace);
	}
	class WorkspaceTouchEvent {
		VelocityTracker mVelocityTracker;
		int lastx;
		int downx =0;
		boolean isdonw = false;
		public boolean onWorspaceTouchEvent(MotionEvent event,Workspace workspace) {
			int e = event.getAction();
		    int x = (int) event.getX();
		    acquireVelocityTrackerAndAddMovement(event);
		    		switch (e) {
					case MotionEvent.ACTION_DOWN:
						Log.e(TAG, "MotionEvent.ACTION_DOWN x=" + x);
						isdonw = true;
						lastx = x;
						downx =x;
						return true;
					case MotionEvent.ACTION_MOVE:
						Log.e(TAG, "MotionEvent.ACTION_MOVE x=" + x);
							int del = lastx - x;
							Log.e(TAG, "downx=" + downx);
							lastx = x;
							//如果是最后一页或者最前一页,滑动时只滑动一半距离
							if(workspace.getmCurrentScreen() == 0 || workspace.getmCurrentScreen() == workspace.getChildCount()-1){
								del /= 2;
							}
							workspace.scrollBy(del, 0);
							workspace.invalidate();
						break;
					case MotionEvent.ACTION_UP:
						Log.e(TAG, "MotionEvent.ACTION_UP x=" + x);
						mVelocityTracker.computeCurrentVelocity(1000);
						Log.e("TAG", "VelocityTracker =" + mVelocityTracker.getXVelocity());
						float xVelocity = mVelocityTracker.getXVelocity();
						int upx = (int) event.getX();
						if((downx - upx ) > workspace.getmWidth() / 3 || (Math.abs(xVelocity) > workspace.SNAP_VELOCITY && xVelocity < 0)){
							workspace.scrollLeft();
						}
						if((downx - upx) < 0 && Math.abs((downx - upx)) > workspace.getmWidth() / 3 || (Math.abs(xVelocity) > workspace.SNAP_VELOCITY && xVelocity > 0)){
							workspace.scrollRight();
						}else{
							workspace.resetScreen();
						}
						downx = 0;
						isdonw = false;
						releaseVelocityTracker();
					default:
						break;
					}
		    		return true;
		}
		private void acquireVelocityTrackerAndAddMovement(MotionEvent ev) {
	        if (mVelocityTracker == null) {
	            mVelocityTracker = VelocityTracker.obtain();
	        }
	        mVelocityTracker.addMovement(ev);
	    }
	    
	    private void releaseVelocityTracker() {
	        if (mVelocityTracker != null) {
	            mVelocityTracker.recycle();
	            mVelocityTracker = null;
	        }
	    }
	}
	
}